package wfmanage.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.wfmanage.models.Lookup;

public class AdminDefineWF extends ActionSupport{
	private String domain;
	private String domain_name="";
	private String commandButton = "";
	private String WFNames="(";
	ArrayList<Lookup> WFPoints = new ArrayList<Lookup>();
	int domain_id=1;
	int WorkflowPoint_id=1;
	private String msg="";
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	 
	public String execute() throws Exception {
		 MyLog.log("Inside AdminDefineWF : Method : execute");

	        HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myDomain")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup domain=Lookup.findOne("domain", "where domain_name = '"+paramVal+"'");
	 	            	domain_id=domain.getId();
	 	            	domain_name=domain.getName();
	 	                MyLog.log(paramVal);
	 	            }
	             }
	             else if(param.equals("myWFPoint")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	WFNames=WFNames+" '"+paramVal+"',";
	 	                MyLog.log(WFNames);
	 	            }
	 	           WFNames=(String)WFNames.subSequence(0, WFNames.length()-1);
	 	            WFNames=WFNames+")";
	 	            this.WFPoints=Lookup.find("WorkflowPoint", " where WorkflowPoint_name in " + WFNames);
	 	            	
	             }
	             
	        }
	
	        if (this.commandButton.startsWith("Add")) {
	        	
           	 	for(int i=0;i<WFPoints.size();i++){
	           		 Lookup list=WFPoints.get(i);
	           		 int WorkflowPoint_id = list.getId();
	           		 
	           		 int result=insert(WorkflowPoint_id);
	           		 if(result>0){
	           			 msg="Added Workflow Point "+list.getName()+" successfully to domain "+domain_name+"!!";
	           			 MyLog.log("inserted wfpoint successfully to domain");
	           		 }
	           		 else{
	           			 msg="Cannot Add, Workflow Point "+list.getName()+" already exists in Domain "+domain_name;
	           			 MyLog.log("Cannot add, wfpoint already exists in this domain");
	           			 return "displayWF";
	           		 }
           	 	}
           	 return "displayWF";
            }
            else if (this.commandButton.startsWith("Remove")) {
           	 for(int i=0;i<WFPoints.size();i++){
           		 Lookup list=WFPoints.get(i);
           		 int WorkflowPoint_id = list.getId();
           		 int result=Lookup.findOnefordelete("domain_workflowpoint"," where WorkflowPoint_id="+WorkflowPoint_id+" and domain_id="+domain_id);
           		 if(result>0){
           			 msg="Removed Workflow Point successfully from domain +"+domain_name+"!!";
           			MyLog.log("Removed Workflow Point "+list.getName() +" successfully from domain"+domain_name);
           		 }
           		 else{
           			msg="Cannot Remove, Workflow Point "+list.getName() +" from Domain "+domain_name+". Please try again!!";
           		 }
           	 }
           	return "displayWF";
            }
            else if (this.commandButton.startsWith("Back to Admin")) {
      			return "admin_home";
      		}
	
	    	return "error";
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	public int insert(int WorkflowPoint_id) {
		String insertSQL = "insert into domain_workflowpoint(domain_id,WorkflowPoint_id) values" +
				"				("+domain_id +", "+WorkflowPoint_id+");";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
	
}
